const { Employee, Department } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
const Sequelize = require('sequelize')
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { employeeName, departmentID, idcard, position, salary, employeeID, searchType, pageNum = 1, // 默认页码为1
        pageSize = 5, contact } = req.body
    try {
        switch (operation) {
            case 'create':
                // // 根据客户端传递过来的 用户名 查询 数据库 中 是否 存在这用户名
                const state = {
                    Name: employeeName,
                    DepartmentID: departmentID,
                    Position: position,
                    Salary: salary,
                    Contact: contact,
                    Idcard: idcard
                }
                const newEmployeeData = await Employee.create(state)
                const data = await Employee.findOne({
                    where: {
                        EmployeeID: newEmployeeData.EmployeeID
                    }
                })
                res.status(200).send({
                    data,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                // 在这里实现获取用户列表的操作
                let whereCondition = {}; // 默认查询条件为空对象
                if (searchType === "exact" && employeeName) {
                    // 执行精确搜索
                    whereCondition = { Name: { [Sequelize.Op.eq]: employeeName } };
                } else if (searchType === "fuzzy" && employeeName) {
                    // 执行模糊搜索
                    whereCondition = { Name: { [Sequelize.Op.like]: `%${employeeName}%` } };
                }

                const total = await Employee.count({ where: whereCondition }); // 根据查询条件获取数据总数

                const offset = (pageNum - 1) * pageSize; // 计算偏移量

                const employeeData = await Employee.findAll(
                    {
                        include: [{
                            model: Department, // 指定要包含的关联模型
                            attributes: ['DepartmentName'], // 可选：指定要包含的部门属性
                        }],
                        where: whereCondition,
                        limit: Number(pageSize),
                        offset: Number(offset),
                    }
                )
                res.status(200).send({
                    data: {
                        employeeData,
                        total,
                        pageSize: Number(pageSize),
                        pageNum: Number(pageNum)
                    },
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'readAll':
                const tableData = await Employee.findAll(
                    {
                        include: [{
                            model: Department, // 指定要包含的关联模型
                            attributes: ['DepartmentName'], // 可选：指定要包含的部门属性
                        }],
                    }
                )
                res.status(200).send({
                    data: {
                        tableData,
                    },
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'update':
                const stateData = {
                    Name: employeeName,
                    DepartmentID: departmentID,
                    Position: position,
                    Salary: salary,
                    Contact: contact,
                    Idcard: idcard
                }
                const upEmployeeData = await Employee.update(stateData, { where: { EmployeeID: employeeID } },)
                res.status(200).send({
                    meta: {
                        msg: "编辑成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                const delData = await Employee.destroy({ where: { EmployeeID: employeeID } })
                console.log(delData);
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'findAllemployeeName':
                // 查询所有的员工名字
                const employeeNameList = await Employee.findAll({ attributes: ['Name'] })
                res.status(200).send({
                    employeeNameList,
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            default:
                break;
        }

    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}